<template>
  <div class="about">
    <h1>{{id ? '编辑' : '添加'}}英雄</h1>
    <div style="margin: 20px;"></div>

    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs value="basic" type="border-card">
        <el-tab-pane label="基本信息" name="basic">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="afterUpload"
            >
              <!-- :action是 发出图片上传请求 
              :on-success 是上传成功是调用的钩子-->
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate v-model="model.scores.difficult" :max="9" show-score style="margin-top:.6rem;"></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate v-model="model.scores.skills" :max="9" show-score style="margin-top:.6rem;"></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate v-model="model.scores.attack" :max="9" show-score style="margin-top:.6rem;"></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate v-model="model.scores.survive" :max="9" show-score style="margin-top:.6rem;"></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option v-for="item of items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option v-for="item of items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips" resize="none"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips" resize="none"></el-input>
          </el-form-item>
          <el-form-item label="团队思路">
            <el-input type="textarea" v-model="model.teamTips" resize="none"></el-input>
          </el-form-item>
        </el-tab-pane>

        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click.prevent="model.skills.push({})">
            <i class="el-icon-plus"></i> 添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap; margin-top:20px;">
            <el-col :sm="12" :md="12" v-for="(item,i) in model.skills" :key="i">
              <el-form-item label="技能名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="技能图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="res => $set(item,'icon', res.url)"
                >
                  <!-- :action是 发出图片上传请求 
                  :on-success 是上传成功是调用的钩子-->
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="技能描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-button type="danger" size="small"
               @click.prevent="model.skills.splice(i,1)"
               style="margin-left:115px;">
                删除
              </el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" native-type="submit" style="margin-top:1rem;margin-left:1rem;">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>



<script>
export default {
  name: "",
  props: {
    id: {}
  },
  data() {
    return {
      items: [],
      labelPosition: "right",
      categories: [],
      model: {
        usageTips: "",
        name: "",
        avatar: "",
        title: "",
        scores: {
          difficult: 0
        },
        skills: []
      }
    };
  },
  methods: {
    afterUpload(res) {
      this.model.avatar = res.url;
    },
    async save() {
      console.log(1);

      let res;
      if (this.id) {
        // 修改
        res = await this.$http.put(`rest/heroes/${this.id}`, this.model);
      } else {
        // 新建
        res = await this.$http.post("rest/heroes", this.model);
      }
      this.$router.push(`/heroes/list`);
      this.$message({
        type: "success",
        message: "保存成功"
      });
      // console.log(res)
    },
    async fetch() {
      // 更新数据
      const res = await this.$http.get(`rest/heroes/${this.id}`);
      this.model = Object.assign({}, this.model, res.data);
    },
    async fetchCategories() {
      // 获取类型列表
      const res = await this.$http.get(`rest/categories`);

      this.categories = res.data;
    },
    async fetchItems() {
      // 获取类型列表
      const res = await this.$http.get(`rest/items`);

      this.items = res.data;
    }
  },
  created() {
    this.fetchItems();
    this.fetchCategories();
    this.id && this.fetch();
  }
};
</script>





<style>

</style>